package com.vcs.frm.service.company;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.vcs.frm.dao.common.CommonDao;
import com.vcs.frm.dao.company.EmployeeDao;
import com.vcs.frm.domain.Employee;
import com.vcs.frm.domain.User;
import com.vcs.frm.domain.UserRole;
import com.vcs.frm.service.common.CommonService;
import com.vcs.frm.utils.Constants;
import com.vcs.frm.web.form.emp.EmployeeRegFormBean;

public class EmployeeServiceImpl implements EmployeeService {

	private final Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private CommonService commonService;

	@Autowired
	private EmployeeDao employeeDao;

	@Autowired
	private CommonDao commonDao;
	
	/* setters for spring dependency injection */
	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}

	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}
	
	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}
	
	/* End setters for spring dependency injection */
	
	@Override
	@Transactional
	public Employee getEmployeeById(Long employeeId) {
		return employeeDao.getEmployeeById(employeeId);
	}
	
	@Override
	@Transactional
	public Employee getEmployeeProfileByUsername(String username) {
		
		User user = commonDao.getUserByUsername(username); 
		Long employeeId = user.getUserId();
		return employeeDao.getEmployeeById(employeeId);  
	}

	@Override
	public Long deleteEmployee(Employee employee) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Long updateEmployee(Employee employee) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@Transactional
	public Long saveEmployee(EmployeeRegFormBean formBean) {

		log.info(formBean);

		User user = new User();
		user.setEnabled(1);
		user.setUsername(formBean.getEmail());
		user.setPassword(formBean.getPassword());

		// create a user
		Long userId = commonService.saveUser(user);
		log.info("userId : " + userId);
		// add role to user
		User u = commonService.getUserById(userId);
		u.getUserRoles().add(new UserRole(userId, Constants.ROLE_EMPLOYEE));
		
		// now save rest of the details in employees table.
		
		return null;
	}


	

}
